#_*_ coding: utf-8 _*_
# Create your views here.
from django.core.mail import send_mail
from django.core.context_processors import csrf
from django.http import HttpResponse
from django.template import loader
from django.template import Context
from django.template import RequestContext
from django.shortcuts import render_to_response
from documentation.forms import ContactForm
from documentation.models import News, Author, Category, Documents, Section

def Hello(request):
    return HttpResponse("<html><body><h4>Hello Django</h4></body></html>")


def MainPage(request):
    menulist=[
        'Первый',
        'Второй',
        'Третий',
        'Четвертый',
        'Пятый'
    ]
    return render_to_response("template.html",locals())


def display_meta(request):
    values = request.META.items()
    values.sort()
    html = []
    for k, v in values:
        html.append('<tr><td>%s</td><td>%s</td></tr>'%(k, v))
    return HttpResponse('<table>%s</table>'%'\n'.join(html))


def contact(request):
    form = ContactForm(request.POST)
    if request.method == 'POST':
        if form.is_valid():
            cd = form.cleaned_data
            send_mail(
                cd['subject'],
                cd['message'],
                cd.get('email','sgalushko@gmail.com'),
                ['galushkoss@savebank.dp.ua']
            )
            return HttpResponse('<h4>Большое спасибо за письмо</h4>')
    else:
        form = ContactForm()
    return render_to_response('contact_form.html',{'form': form},context_instance=RequestContext(request))


def SiteNews(request):
    Posts=News.objects.order_by("add_date")
    return render_to_response('main.html',{'page_title':u'Новости сайта','page_brand':u'Новости','posts':Posts},context_instance=RequestContext(request))


def Docums(request):
    Docs = Documents.objects.order_by('title')
    Menu = {}
    Categs = Category.objects.order_by("title")
    Sects = Section.objects.order_by("category","title")
    for categ in Categs:
        Menu[categ] = Section.objects.filter(category__title=categ)
    return render_to_response("newdocs.html",{'page_title':u'Документація','page_brand':u'Документація','Docs':Docs,'menu':Menu},context_instance=RequestContext(request))